package com.leyou.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.leyou.common.enums.ExceptionEnum;
import com.leyou.common.exceptions.LyException;
import com.leyou.common.utils.BeanHelper;
import com.leyou.common.vo.PageResultVO;
import com.leyou.item.dto.BrandDTO;
import com.leyou.item.entity.Brand;
import com.leyou.service.IBrandService;
import com.leyou.mapper.IBrandMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;

import java.util.List;

/**
 * @author xiuer
 * @description 品牌业务层
 * @create 2019/6/24 16:14
 */
@Service
public class BrandServiceImpl implements IBrandService {
    @Autowired
    private IBrandMapper brandMapper;

    /**
     * 品牌条件分页查询
     */
    @Override
    public PageResultVO<BrandDTO> brandQueryPage(String key, Integer page, Integer rows, String sortBy, Boolean desc) {
        //分页
        PageHelper.startPage(page, rows);
        List<Brand> brands = null;
        //条件查询品牌的数据
        if (desc) {
            //降序
            brands = brandMapper.queryBrandSortList(key, sortBy, "DESC");
        } else {
            //升序
            brands = brandMapper.queryBrandSortList(key, sortBy, "ASC");
        }
        if (CollectionUtils.isEmpty(brands)) {
            //查不到数据404
            throw new LyException(ExceptionEnum.BRAND_NOT_FOUND);
        }
        //获取分页数据
        PageInfo<Brand> pageInfo = new PageInfo<>(brands);
        PageResultVO<BrandDTO> pageVO = new PageResultVO<>();
        pageVO.setItems(BeanHelper.copyWithCollection(pageInfo.getList(), BrandDTO.class));
        pageVO.setTotal(pageInfo.getTotal());
        return pageVO;
    }

    /**
     * 新增品牌
     * 新增商品品牌的中间表
     * cid 分类主键   bid 品牌主键
     */
    @Override
    @Transactional
    public void addBrand(BrandDTO brandDTO, List<Long> cids) {
        Brand brand = BeanHelper.copyProperties(brandDTO, Brand.class);
        int resultRows = brandMapper.insertBrand(brand);
        if (resultRows != 1) {
            throw new LyException(ExceptionEnum.INSERT_OPERATION_FAIL);
        }
        resultRows = brandMapper.insertCidsAndBid(cids, brand.getId());
        if (resultRows != cids.size()) {
            throw new LyException(ExceptionEnum.INSERT_OPERATION_FAIL);
        }
    }

    /**
     * 修改商品分类和品牌
     * 品牌和分类的中间表采用先删除在新增的操作
     *
     * @param brandDTO
     * @param cids
     */
    @Override
    @Transactional
    public void updateBrandAndCategory(BrandDTO brandDTO, List<Long> cids) {
        Brand brand = BeanHelper.copyProperties(brandDTO, Brand.class);
        //修改品牌
        int resultRows = brandMapper.updateBrand(brand);
        if (resultRows == 0) {
            //更新失败
            throw new LyException(ExceptionEnum.UPDATE_OPERATION_FAIL);
        }
        //更新成功删除中间表关于该品牌的所有分类信息（根据品牌主键id）
        resultRows = brandMapper.deleteCategoryBrandByBid(brand.getId());
        if (resultRows == 0) {
            //同属于更新操作，操作失败抛更新失败异常
            throw new LyException(ExceptionEnum.DELETE_OPERATION_FAIL);
        }
        //新增品牌和商品分类信息
        resultRows = brandMapper.insertCidsAndBid(cids, brand.getId());
        if (resultRows != cids.size()) {
            throw new LyException(ExceptionEnum.INSERT_OPERATION_FAIL);
        }
    }

    /**
     * 删除品牌及该品牌的所有分类
     */
    @Override
    @Transactional
    public void deleteBrandByBid(Long bid) {
        //删除品牌
        int resultRows = brandMapper.deleteBrandByid(bid);
        if (resultRows == 0) {
            //删除失败
            throw new LyException(ExceptionEnum.DELETE_OPERATION_FAIL);
        }
        //删除该品牌的分类
        resultRows = brandMapper.deleteCategoryBrandByBid(bid);
        if (resultRows == 0) {
            //删除失败
            throw new LyException(ExceptionEnum.DELETE_OPERATION_FAIL);
        }
    }

    /**
     * 根据品牌id 查询品牌
     *
     * @param bid
     * @return
     */
    @Override
    public BrandDTO queryBrandByBid(Long bid) {

        Brand brand = brandMapper.queryBrandByBid(bid);
        if (brand == null) {
            throw new LyException(ExceptionEnum.BRAND_NOT_FOUND);
        }
        return BeanHelper.copyProperties(brand, BrandDTO.class);
    }

    /**
     * 根据三级分类id查询所有品牌信息
     */
    @Override
    public List<BrandDTO> queryBrandBycid(Long cid) {
        List<Brand> list = brandMapper.queryBrandBycid(cid);
        if (CollectionUtils.isEmpty(list)) {
            throw new LyException(ExceptionEnum.BRAND_NOT_FOUND);
        }
        return BeanHelper.copyWithCollection(list, BrandDTO.class);
    }

    /**
     * 根据品牌id集合查询品牌
     */
    @Override
    public List<BrandDTO> queryBrandByBids( List<Long> bids) {
        List<Brand> list = brandMapper.queryBrandByBids(bids);
        if (CollectionUtils.isEmpty(list)) {
            throw new LyException(ExceptionEnum.BRAND_NOT_FOUND);
        }
        return BeanHelper.copyWithCollection(list, BrandDTO.class);
    }
}
